package com.blog.controller.frontpage;



import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.blog.controller.base.BaseServlet;
import com.blog.dao.frontpage.PersoncenterDao;
import com.blog.entity.ArticleEntity;
import com.blog.entity.BolgerArticleEntity;
import com.blog.entity.UserEntity;
import com.blog.entity.UserLoginEntity;
import com.blog.service.frontpage.PersonalCenterService;
import com.blog.utils.PageBean;

@WebServlet("/personalcenter")
public class PersonalCenterSevlet extends BaseServlet{
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int userid=1;
		UserLoginEntity user=(UserLoginEntity )req.getSession().getAttribute("user");
		if(user!=null) {
			userid=user.getUser_id();
		}
		System.out.println("userid"+userid);
		PersonalCenterService service=PersonalCenterService.getInstance();
		int pageNum=req.getParameter("pageNum")==null?1:Integer.parseInt(req.getParameter("pageNum"));
		int pageSize=req.getParameter("pageSize")==null?5:Integer.parseInt(req.getParameter("pageSize"));
		int totalRecord;
		if(req.getParameter("totalRecord")==null) {
			totalRecord=(int) service.getTotalNum(userid, 0).getTotalNum();
		}
		else {
			totalRecord=Integer.parseInt(req.getParameter("totalRecord"));
		}
		PageBean<BolgerArticleEntity> pageBean=new PageBean<BolgerArticleEntity>(pageNum,pageSize,totalRecord);
		List<BolgerArticleEntity> articles=service.getArticleEntity(userid,pageBean.getStartIndex(),pageBean.getPageSize());
		pageBean.setList(articles);
		req.setAttribute("pageBean", pageBean);
		forward("frontpage/personalcenter");
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
}
